package zzs.generalframework.console.core.parameter.commonparameter.politicalstatus.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.politicalstatus.PoliticalStatusBO;
import zzs.generalframework.common.core.parameter.commonparameter.politicalstatus.PoliticalStatusInfo;
import zzs.generalframework.common.core.parameter.commonparameter.politicalstatus.PoliticalStatusPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.politicalstatus.dao.IPoliticalStatusDao;
import zzs.generalframework.console.core.parameter.commonparameter.politicalstatus.service.IPoliticalStatusService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.politicalstatus.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.politicalstatus.service.impl.politicalStatusServiceImpl")
public class PoliticalStatusServiceImpl implements IPoliticalStatusService
{

	private Logger logger = LoggerFactory.getLogger(PoliticalStatusServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IPoliticalStatusDao politicalStatusDao;

	@Override
	public ConsoleReturnBO savePoliticalStatus(PoliticalStatusBO politicalStatusBO)
	{
		logger.debug("====保存政治面貌信息开始====");
		logger.debug("====保存政治面貌信息.入参:PoliticalStatusBO=[{}]====", politicalStatusBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		PoliticalStatusPO politicalStatusPO = new PoliticalStatusPO();
		copyPoliticalStatusBO2PoliticalStatusPO(politicalStatusBO, politicalStatusPO);
		Boolean insertResult = politicalStatusDao.insert(politicalStatusPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存政治面貌信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存政治面貌信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO savePoliticalStatus(List<PoliticalStatusBO> politicalStatusBOList) throws ConsoleException
	{
		logger.info("====保存政治面貌信息列表开始====");
		logger.debug("====保存政治面貌信息列表.入参:PoliticalStatusBOList=[{}]====", politicalStatusBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (PoliticalStatusBO itemBO : politicalStatusBOList)
		{
			ConsoleReturnBO itemResult = savePoliticalStatus(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存政治面貌信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存政治面貌信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getPoliticalStatus(PoliticalStatusInfo politicalStatusInfo)
	{
		logger.info("====获取政治面貌信息开始====");
		logger.debug("====获取政治面貌信息.入参:PoliticalStatusInfo=[{}]====", politicalStatusInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<PoliticalStatusPO> politicalStatusPOList = politicalStatusDao.select(politicalStatusInfo);
		if (politicalStatusPOList == null || politicalStatusPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (politicalStatusPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			PoliticalStatusBO politicalStatusBO = new PoliticalStatusBO();
			copyPoliticalStatusPO2PoliticalStatusBO(politicalStatusPOList.get(0), politicalStatusBO);
			returnConsoleReturnBO.setReturnObj(politicalStatusBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			PoliticalStatusBO politicalStatusBO = new PoliticalStatusBO();
			copyPoliticalStatusPO2PoliticalStatusBO(politicalStatusPOList.get(0), politicalStatusBO);
			returnConsoleReturnBO.setReturnObj(politicalStatusBO);
		}
		logger.debug("====获取政治面貌信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取政治面貌信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listPoliticalStatus(PoliticalStatusInfo politicalStatusInfo)
	{
		logger.info("====获取政治面貌信息列表开始====");
		logger.debug("====获取政治面貌信息列表.入参:PoliticalStatusInfo=[{}]====", politicalStatusInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<PoliticalStatusPO> politicalStatusPOList = politicalStatusDao.select(politicalStatusInfo);
		if (politicalStatusPOList == null || politicalStatusPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<PoliticalStatusBO> resultList = new ArrayList<PoliticalStatusBO>();
			for (PoliticalStatusPO itemPO : politicalStatusPOList)
			{
				PoliticalStatusBO itemBO = new PoliticalStatusBO();
				copyPoliticalStatusPO2PoliticalStatusBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取政治面貌信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取政治面貌信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updatePoliticalStatus(PoliticalStatusBO politicalStatusBO)
	{
		logger.info("====更新政治面貌信息开始====");
		logger.debug("====更新政治面貌信息.入参:PoliticalStatusBO=[{}]====", politicalStatusBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		PoliticalStatusPO politicalStatusPO = new PoliticalStatusPO();
		copyPoliticalStatusBO2PoliticalStatusPO(politicalStatusBO, politicalStatusPO);
		Boolean updateResult = politicalStatusDao.update(politicalStatusPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新政治面貌信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新政治面貌信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updatePoliticalStatus(List<PoliticalStatusBO> politicalStatusBOList) throws ConsoleException
	{
		logger.info("====更新政治面貌信息列表开始====");
		logger.debug("====更新政治面貌信息列表.入参:List<PoliticalStatusBO>=[{}]====", politicalStatusBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (PoliticalStatusBO itemBO : politicalStatusBOList)
		{
			ConsoleReturnBO itemResult = updatePoliticalStatus(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新政治面貌信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新政治面貌信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removePoliticalStatus(PoliticalStatusBO politicalStatusBO)
	{
		logger.info("====删除政治面貌信息开始====");
		logger.debug("====删除政治面貌信息.入参:PoliticalStatusBO＝[{}]======", politicalStatusBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		PoliticalStatusPO politicalStatusPO = new PoliticalStatusPO();
		copyPoliticalStatusBO2PoliticalStatusPO(politicalStatusBO, politicalStatusPO);
		Boolean deleteResult = politicalStatusDao.delete(politicalStatusPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除政治面貌信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除政治面貌信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removePoliticalStatus(List<PoliticalStatusBO> politicalStatusBOList) throws ConsoleException
	{
		logger.info("====删除政治面貌信息列表开始====");
		logger.debug("====删除政治面貌信息列表.入参:List<PoliticalStatusBO>＝[{}]======", politicalStatusBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (PoliticalStatusBO itemBO : politicalStatusBOList)
		{
			ConsoleReturnBO itemResult = removePoliticalStatus(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除政治面貌信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除政治面貌信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countPoliticalStatus(PoliticalStatusBO politicalStatusBO)
	{
		logger.info("====统计政治面貌信息开始====");
		logger.debug("====统计政治面貌信息。入参：PoliticalStatusBO＝[{}]======", politicalStatusBO);
		Long returnLong = new Long(0);
		PoliticalStatusPO politicalStatusPO = new PoliticalStatusPO();
		copyPoliticalStatusBO2PoliticalStatusPO(politicalStatusBO, politicalStatusPO);
		returnLong = politicalStatusDao.count(politicalStatusPO);
		logger.debug("====统计政治面貌信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计政治面貌信息结束====");
		return returnLong;
	}

	private void copyPoliticalStatusBO2PoliticalStatusPO(PoliticalStatusBO politicalStatusBO, PoliticalStatusPO politicalStatusPO)
	{
		beanUtils.copyBeanNotNull(politicalStatusBO, politicalStatusPO);
		if (politicalStatusBO.getSysID() != null)
		{
			politicalStatusPO.setSeqID(politicalStatusBO.getSysID());
		}
	}

	private void copyPoliticalStatusPO2PoliticalStatusBO(PoliticalStatusPO politicalStatusPO, PoliticalStatusBO politicalStatusBO)
	{
		beanUtils.copyBeanNotNull(politicalStatusPO, politicalStatusBO);
		if (politicalStatusPO.getSeqID() != null)
		{
			politicalStatusBO.setSysID(politicalStatusPO.getSeqID());
		}
	}

}
